package controller.link.game;

import view.client.Console;
import model.Hearer;
import view.client.PanelGame;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import controller.Link;
import model.game.Place;
import model.game.Scenario;
import model.User;
import model.Attendant;
import model.Server;

public class Ask4Neighboors implements Link, Serializable {

    private ArrayList<String> locations = new ArrayList<String>();

    public Ask4Neighboors() {
    }

    public Ask4Neighboors(Hearer h) {
        h.send((Link) this);
    }

    @Override
    public void serverUse(Server server, Attendant at) {
        User user = at.getUser();
        Object o = server.getWorld().getMap().whereIsPlayer(user.getPlayer().getName());
        if (o == null) {
            return;
        }
        if (o instanceof Place) {
            Place p = (Place) o;
            locations.add(server.getWorld().getMap().whatScenarioContainsPlace(p.getName()).getName());
        } else if (o instanceof Scenario) {
            Scenario scenario = (Scenario) o;
            String[] names = server.getWorld().getMap().findNeighboors(scenario);
            locations.addAll(Arrays.asList(names));
        }
        at.send((Link) this);
    }

    @Override
    public void clientUse(Console console) {
        PanelGame pg = console.getPg();
        pg.getModelPlace().removeAllElements();
        for (int i = 0; i < locations.size(); i++) {
            if (locations.get(i) != null) {
                pg.getModelPlace().add(i, locations.get(i));
            }
        }
        pg.getListAction().revalidate();
        pg.getListAction().repaint();
        pg.getListPlace().revalidate();
        pg.getListPlace().repaint();
        pg.revalidate();
        console.getFrame().repaint();
    }
}
